package ch.mylonas.saometer.data;

import processing.core.PApplet;


public abstract class SerialDataCommon {

	protected Integer nrOfDataPoints;

	protected final static int NUMBER_OF_CHANNELS = 2;

	protected int[][] data;
	PApplet myParent;
	Boolean isRunning;
	int countDown = -1;

	public SerialDataCommon() {
		super();
	}

	protected void resetData(){
		// fill in the array with 0
		for(int i=0; i<this.nrOfDataPoints; i++){
			for(int k=0; k<NUMBER_OF_CHANNELS; k++){
				data[k][i] = 0;
			}
		}
	}

	protected void shiftArray(int[] newData){
		// shift the array
		
		if(newData.length != NUMBER_OF_CHANNELS){
			throw new RuntimeException("Wrong number of data-points: " + newData.length + " instead of " + NUMBER_OF_CHANNELS);
		}

		for(int i=1; i <= this.nrOfDataPoints-1; i++){
			for(int k=0; k<NUMBER_OF_CHANNELS; k++){
				data[k][i-1] = this.data[k][i];
			}
		}

		for(int k=0; k<NUMBER_OF_CHANNELS; k++){
			data[k][this.nrOfDataPoints-1] = newData[k];
		}
		
		if(countDown > 0){
			countDown--;
		}else if(countDown == 0){
			this.isRunning = false;
			countDown--;
		}

	}

	public PApplet getMyParent() {
		return myParent;
	}

	public void setMyParent(PApplet myParent) {
		this.myParent = myParent;
	}
	
	public void isRunning(boolean isRunning){
		this.isRunning = isRunning;
	}
	
	public boolean isRunning(){
		return this.isRunning;
	}
	
	public void startCountdown(int countDown){
		this.countDown = countDown;
	}

}
